using SudokuHelper.Model;
using SudokuHelper.Model.Values;
using SudokuHelper.Views;

namespace SudokuHelper.Controllers {
    public class SudokuController : ISudokuController {
        #region Private members

        private SudokuDesk desk;

        #endregion

        #region Properties

        protected SudokuDesk SudokuDesk {
            get { return desk;}
        }

        #endregion

        #region Constructor

        public SudokuController() {
            this.desk = new SudokuDesk();
        }

        public SudokuController(SudokuDesk desk) {
            this.desk = desk;
        }

        #endregion

        #region Implementation of ISudokuController

        public ISudokuDesk Desk {
            get { return desk;}
        }

        public ISudokuView View { get; set;}

        public bool CellIsFree(int rowIndex, int colIndex) {
            IValue v = desk[rowIndex, colIndex];
            return v == null || (v is ValueRange);
        }

        public void SetValue(int rowIndex, int colIndex, IValue value) {
            if (desk.CheckItemValid(rowIndex, colIndex, value)) {
                desk[rowIndex, colIndex] = value;
            } 
            else
                View.CellError(rowIndex, colIndex, value);
        }

        public void Reset() {
            desk = new SudokuDesk();
            View.Reset();
        }

        #endregion
    }
}